// util.js

/**
 * 获取当前时间，格式为 YYYY-MM-DD HH:mm:ss
 * @returns {string} 格式化的当前时间
 */
function getFormattedCurrentTime() {
  const date = new Date();
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 计算当前时间与传入时间的差值
 * @param {string} inputTime 输入时间，格式为 'YYYY-MM-DD HH:mm:ss'
 * @returns {string} 返回差值的描述，如 '一天前', '1小时前', '30分钟前'
 */
function getTimeDifference(inputTime) {
  const now = new Date();
  const inputDate = new Date(inputTime);

  const timeDiff = now - inputDate; // 毫秒差值

  if (timeDiff < 0) {
    return "输入的时间不能晚于当前时间";
  }

  const seconds = Math.floor(timeDiff / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);

  if (days > 0) {
    return `${days}天前`;
  } else if (hours > 0) {
    return `${hours}小时前`;
  } else if (minutes > 0) {
    return `${minutes}分钟前`;
  } else {
    return `${seconds}秒前`;
  }
}

// 导出方法
export { getFormattedCurrentTime, getTimeDifference };
